﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kugar.AdminLte.Core.Test.Managers;
using Kugar.Core.Configuration;
using Kugar.Core.ExtMethod;
using Kugar.Core.Web;
using Kugar.UI.Web.AdminLTE;
using Kugar.UI.Web.AdminLTE.Authentications;
using Kugar.UI.Web.AdminLTE.Helpers;
using Kugar.UI.Web.AdminLTE.Interfaces;
using Kugar.UI.Web.AdminLTE.PageControls;
using Kugar.UI.Web.AdminLTE.RequestValidate;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;

namespace Kugar.AdminLte.Core.Test
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            //AdminLTEUI.LayoutPath.ResourceBaseUrl=""
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => false;
                options.MinimumSameSitePolicy = SameSiteMode.Lax;
                //options.ConsentCookie.Path = "/;SameSite=None; Secure";
            });
            
            services.AddSingleton<IAdminLoginManager>(new AdminManager());
            services.AddSingleton<IAdminManager>(new AdminManager());


            services.AddHttpContextAccessor();
            services.UseAdminLTE(false);

            services.UseAdminLTERequestValidate();



            services.AddAuthentication()
                .AddAdminLTEJWT("system", "system", new AdminJWTOption()
                {
                    LoginManager = new AdminManager(),
                    MainTitle = "测试管理后台",
                    TokenEncKey =CustomConfigManager.Default["Api"]

                })
                .AddAdminLTEJWT("admin", "admin", new AdminJWTOption()
                {
                    LoginManager = new AdminManager(),
                    MainTitle = "测试管理后台2222",
                    TokenEncKey = "123456"
                });

            //services.AddAuthentication()
            //    .AddDefaultAdminLTECookie()
            //    .AddAdminLTECookie("system", option =>
            //    {
                    
            //        option.LoginManager = new AdminManager();
            //        option.MainTitle = "测试管理后台";
            //        //option.LogoUrl="";
            //        option.RegisterUrl = "注册";
            //        option.ForgetpasswordUrl = "忘记密码";
            //        option.loginPageManager=new AdminManager();
            //        //option.DefaultUserID = CustomConfigManager.Default["AdminLTE:DefaultUserID"].ToStringEx();
            //    });

            //AdminLTEUI.JqTableEndabled = false;

            // 3.0 使用该段
            //services.AddHttpContextAccessor();

            //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);//.AddSessionStateTempDataProvider();

            services.AddControllersWithViews();
            //services.AddSession(options =>
            //{
            //    // Set a short timeout for easy testing.
            //    options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
            //    options.Cookie.Name = "kugarlte";
            //    options.IdleTimeout = TimeSpan.FromMinutes(15);
            //    options.Cookie.HttpOnly = false;
            //});


        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseRouting();

            //app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            
            app.UseAdminLTERequestValidateStaticFile();
            
            app.UseAdminLTEStaticFile();

            app.UseAuthorization();
            
            app.UseStaticHttpContext();

            //app.UseMvc(routes =>
            //{
            //    routes.MapRoute(
            //        name: "areas",
            //        template: "{area:exists}/{controller=Home}/{action=Index}");

            //    routes.MapRoute(
            //        name: "default",
            //        template: "{controller=Home}/{action=Index}");
            //});

            //3.0 使用该段
            //app.UseEndpoints(endpoints =>
            //{
            //    endpoints.MapControllerRoute(
            //        name: "areas",
            //        pattern: "{area}/{controller=Home}/{action=Index}");

            //    endpoints.MapControllerRoute(
            //        name: "default",
            //        pattern: "{controller=Home}/{action=Index}/{id?}");


            //});

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "areas",
                    pattern: "{area}/{controller=Home}/{action=Index}");

                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}");


            });
        }


    }

}
